﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PhotoGallery.BLL;
using PhotoGallery.Model;
    public partial class Server : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.Form["index"]))
            {
                GetImageInfo();
            }
            if (!string.IsNullOrEmpty(Request["username"]))
            {
                checkUserName();
            }
            if (!string.IsNullOrEmpty(Request["nickname"]))
            {
                checkNickName();
            }
            if (!string.IsNullOrEmpty(Request["name"])) 
            {
                existUserName();
            }
        }

        private void existUserName()
        {
            string username = Request["name"];
            RegistAction r = new RegistAction();
            if (!r.isExist(username))
            {
                Response.Write("用户名不存在！");
                Response.End();
            }
            else
            {
                Response.Write(username);
                Response.End();
            }
        }
        private void checkUserName()
        {
            string username = Request["username"];
            RegistAction r=new RegistAction();
            if (r.isExist(username))
            {
                Response.Write("该邮箱已被使用！请更换！");
                Response.End();
            }
            else {
                Response.Write(username);
                Response.End();
            }
        }

        private void checkNickName()
        {
            string nickname = Request["nickname"];
            RegistAction r = new RegistAction();
            if (r.isNickExist(nickname))
            {
                Response.Write("该昵称已被使用！请更换！");
                Response.End();
            }
            else
            {
                Response.Write(nickname);
                Response.End();
            }
        }

        private void GetImageInfo()
        {
            try
            {   string type = Request.Form["type"].ToString();//区分请求来源
                string index =Request.Form["index"].ToString();//请求所提供的参数
                switch(type)
                {
                    case "album": //相册模式
                        showalbumpic(index);
                        break;
                    case "all"://显示全部图片
                        showallpic();
                        break;
                    case "user"://显示用户的全部图片
                        showalluserpic(index);
                        break;
                    case "favor"://显示收藏的全部图片
                        showalluserfavor(index);
                        break;
                    default:
                        break;
                }
            }
            catch (System.Threading.ThreadAbortException ex)
            {
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        //显示用户喜爱的图片
        private void showalluserfavor(string index)
        {
            CommentAction ca = new CommentAction();
            data(ca.getmyfavor(index));
        }
        //显示用户的所有图片
        private void showalluserpic(string index)
        {
            PhotoAction pa = new PhotoAction();
            data(pa.showAllPhotosofSomeOne(index));
        }
        //显示全站图片
        private void showallpic()
        {
            PhotoAction pa = new PhotoAction();
            data(pa.showAllPhotos());
        }
        //显示某相册的全部图片
        private void showalbumpic(string index)
        {
            PhotoAction pa = new PhotoAction();
            data(pa.showAllPhotosofAlbum(index));
        }
        private void data(List<Picture> picList) 
        {
            if (picList == null)
            {
                Response.Write("fail in null");
                Response.End();
            }
            else
            {
                if (picList.Count > 0)
                {
                    string strRe = "[{Pic:'" + picList[0].Pic + "',Desp:'" + picList[0].Desp +"',Pid:'"+picList[0].Id+ "'}";
                    picList.RemoveAt(0);
                    foreach (Picture p in picList)
                    {
                        strRe += ",{Pic:'" + p.Pic + "',Desp:'" + p.Desp + "',Pid:'"+p.Id+"'}";

                    }
                    strRe += "]";
                    Response.Write(strRe);
                    Response.End();
                }
                else
                {
                    Response.Write("fail in count");
                    Response.End();
                }
            }
        }

    }